package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断购物车是否已经有该商品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.select(shoppingCart);
        if (shoppingCarts != null && shoppingCarts.size()>0){
            ShoppingCart shoppingCart1 = shoppingCarts.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1); //update shopping-cart set number = ? where id = ?
            shoppingCartMapper.updateById(shoppingCart1);

        } else{
                //如果商品不存在  则插入数据 并判断是菜品 还是套餐
               if (shoppingCartDTO.getSetmealId() != null) {
                   //添加的是套餐
                   Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                   shoppingCart.setImage(setmeal.getImage());
                   shoppingCart.setCreateTime(LocalDateTime.now());
                   shoppingCart.setNumber(1);
                   shoppingCart.setAmount(setmeal.getPrice());
                   shoppingCart.setName(setmeal.getName());
                   shoppingCartMapper.insert(shoppingCart);
               }else {
                   //添加的是菜品
                   Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                   shoppingCart.setImage(dish.getImage());
                   shoppingCart.setCreateTime(LocalDateTime.now());
                   shoppingCart.setName(dish.getName());
                   shoppingCart.setAmount(dish.getPrice());
                   shoppingCart.setNumber(1);
                   shoppingCartMapper.insert(shoppingCart);

               }
            }





    }

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.select(shoppingCart);
        return shoppingCarts;
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.select(shoppingCart);
        ShoppingCart shoppingCart1 = shoppingCarts.get(0);
        shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
        shoppingCartMapper.updateById(shoppingCart1);
        if (shoppingCart1.getNumber() == 0){
            shoppingCartMapper.delete(shoppingCart1);
        }
    }

    @Override
    public void clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delete(shoppingCart);
    }
}
